package logic

import (
	"context"
	"fmt"
	"jingdong-product/models/mysql"

	"jingdong-product/productrpc/internal/svc"
	"jingdong-product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// productAdd
func (l *ProductAddLogic) ProductAdd(in *productrpc.ProductAddRequest) (*productrpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		ProductTitle: in.ProductTitle,
		ProductPrice: float64(in.ProductPrice),
		ProductImage: in.ProductImage,
		ProductInfo:  in.ProductInfo,
		UserId:       int(in.UserId),
	}
	err := mysql.DB.Debug().Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &productrpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
